import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, Subject } from 'rxjs';
import { map } from 'rxjs/operators';
import { Schedule } from './schedule.model';

@Injectable()
export class ScheduleService {

    constructor(private http: HttpClient) { }

    /**列出全部分组 */
    listGroups() {
        return this.http.get('/interface/system/list').pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 根据分组名称获取定时任务 */
    listScheduleByGroupName(groupName: string) {
        return this.http.get('/schedule/api/listbygroupname?groupName=' + groupName).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 显示新增/修改界面 */
    showJobDetail(groupName: string, jobName: string) {
        return this.http.post('/schedule/api/showjobdetail', { 'groupName': groupName, 'jobName': jobName }).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 更新定时任务 */
    saveUpdateSchedule(schedule: Schedule) {
        return this.http.post('/schedule/api/saveupdate', schedule).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 删除定时任务 */
    deleteSchedule(schedule: Schedule) {
        return this.http.post('/schedule/api/delete', schedule).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 暂停定时任务 */
    pauseSchedule(schedule: Schedule) {
        return this.http.post('/schedule/api/pause', schedule).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 重新启动定时任务 */
    resumeSchedule(schedule: Schedule) {
        return this.http.post('/schedule/api/resume', schedule).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 重新启动定时任务 */
    runJobOne(schedule: Schedule) {
        return this.http.post('/schedule/api/runonce', schedule).pipe(
            map((res: Response) => res['data'])
        )
    }

}